Ext.define('YXMES.smfr.controller.WorkOrderController', {
	extend : 'Ext.app.Controller',
	refs : [ {
		selector : 'smfr_workorder',
		ref : 'smfrworkorder'
	} ],
	requires : [ 'YXMES.smfr.store.WorkOrderStore', 'YXMES.smfr.store.PathOperationStore', 'YXMES.smfr.store.ProductListStore', 'YXMES.smfr.store.CdsMoStore', 'YXMES.smfr.store.WorkShopStore', 'YXMES.smfr.store.ProductionLineStore', 'YXMES.smfr.store.ProductProcessPathStore' ],
	stores : [],
	models : [],
	views : [ 'YXMES.smfr.view.WorkOrderView' ],
	subcontrollers : [  ],
	_init : false,
	init : function() {
		if (this._init)
			return;
		this._init = true;
		this.control({
			'smfr_workorder' : {
				afterrender : function() {
					return this.handle(this.onAfterrender, arguments);
				},
				destroy : function() {
					return this.handle(this.onDestroy, arguments);
				}
			},
			'smfr_workorder #cdvMatCode' : {
				select : function() {
					return this.handle(this.onCdvMatCodeSelect, arguments);
				}
			},
			'smfr_workorder #cmbShopCode' : {
				select : function() {
					return this.handle(this.onCmbShopCodeSelect, arguments);
				}
			},
			'smfr_workorder #cmbPathCode' : {
				change : function() {
					return this.handle(this.onCmbPathCodeChange, arguments);
				}
			},
			'smfr_workorder #btnSearch' : {
				click : function() {
					return this.handle(this.onBtnSearchClick, arguments);
				}
			},
			'smfr_workorder #btnSearchReset' : {
				click : function() {
					return this.handle(this.onBtnSearchResetClick, arguments);
				}
			},
			'smfr_workorder #grdMo' : {
				itemclick : function() {
					return this.handle(this.onGrdMoItemclick, arguments);
				}
			},
			'smfr_workorder #btnReset' : {
				click : function() {
					return this.handle(this.onBtnResetClick, arguments);
				}
			},
			'smfr_workorder #btnSave' : {
				click : function() {
					return this.handle(this.onBtnSaveClick, arguments);
				}
			},
			'smfr_workorder #btnPrint' : {
				click : function() {
					return this.handle(this.onBtnPrintClick, arguments);
				}
			}
		});
		if (!Ext.isEmpty(this.subcontrollers)) {
			Ext.each(this.subcontrollers, function(name) {
				this.getController(name).init();
			}, this);
		}
		// User definied init
	},
	mainView : undefined,
	getMainView : function() {
		return this.mainView;
	},
	getScopeView : function() {
		return this.getMainView().scopeView || {};
	},
	getInput : function() {
		return this.getScopeView().input || {};
	},
	setInput : function(data) {
		this.getMainView().input = data;
	},
	getOutput : function() {
		return this.getMainView().output || {};
	},
	setOutput : function(data) {
		this.getScopeView().output = data;
	},
	handle : function(func, args) {
		var item = args && args.length > 0 && Ext.typeOf(args[0]) === 'object' ? args[0] : undefined;
		if (item) {
			try {
				this.mainView = (item.xtype == 'smfr_workorder' ? item : item.up('smfr_workorder'));
			} catch(e) {
			}
		}
		try {
			return func.apply(this, args);
		} finally {
		}
	},
	getItem : function(itemId) {
		return itemId===this.getMainView().getItemId()?this.getMainView():this.getMainView().down('#' + itemId);
	},
	getStore : function(name) {
		if (name == 'WorkOrderStore')
			return this.getMainView().SmfrViewProductionOrderServiceStore;
		if (name == 'PathOperationStore')
			return this.getMainView().SmfrViewProcessPathOperationServiceStore;
		if (name == 'ProductListStore')
			return this.getMainView().SbasViewProductServiceStore;
		if (name == 'CdsMoStore')
			return this.getMainView().BasViewCommonDataSetupDetailServiceStore;
		if (name == 'WorkShopStore')
			return this.getMainView().SbasViewWorkshopServiceStore;
		if (name == 'ProductionLineStore')
			return this.getMainView().SbasViewProductionLineServiceStore;
		if (name == 'ProductProcessPathStore')
			return this.getMainView().SbasViewProductProcessPathServiceStore;
		return undefined;
	},
	bind : function(view) {
		view = (view ? view : this.getMainView());
		view.SmfrViewProductionOrderServiceStore = Ext.create('YXMES.smfr.store.WorkOrderStore', {
			pageSize : 30
		});
		view.SmfrViewProcessPathOperationServiceStore = Ext.create('YXMES.smfr.store.PathOperationStore', {
			pageSize : 30
		});
		view.SbasViewProductServiceStore = Ext.create('YXMES.smfr.store.ProductListStore', {
			pageSize : 30
		});
		view.BasViewCommonDataSetupDetailServiceStore = Ext.create('YXMES.smfr.store.CdsMoStore', {
			pageSize : 30
		});
		view.SbasViewWorkshopServiceStore = Ext.create('YXMES.smfr.store.WorkShopStore', {
			pageSize : 30,
			autoDestory : true,
			autoLoad : true
		});
		view.SbasViewProductionLineServiceStore = Ext.create('YXMES.smfr.store.ProductionLineStore', {
			pageSize : 30,
			autoDestory : true
		});
		view.SbasViewProductProcessPathServiceStore = Ext.create('YXMES.smfr.store.ProductProcessPathStore', {
			pageSize : 30
		});
		view.down('#grdMo').reconfigure(view.SmfrViewProductionOrderServiceStore);
		view.down('#bar6').bindStore(view.SmfrViewProductionOrderServiceStore);
		view.down('#grdPathOperation').reconfigure(view.SmfrViewProcessPathOperationServiceStore);
		view.down('#cdvMatCode').bindStore(view.SbasViewProductServiceStore);
		view.down('#cmbMoType').bindStore(view.BasViewCommonDataSetupDetailServiceStore);
		view.down('#cmbMoStatus').bindStore(view.BasViewCommonDataSetupDetailServiceStore);
		view.down('#cmbShopCode').bindStore(view.SbasViewWorkshopServiceStore);
		view.down('#cmbLineCode').bindStore(view.SbasViewProductionLineServiceStore);
		view.down('#cmbPathCode').bindStore(view.SbasViewProductProcessPathServiceStore);
		view.down('#cmbSearchMoStatus').bindStore(view.BasViewCommonDataSetupDetailServiceStore);
		view.down('#cdvSearchMatCode').bindStore(view.SbasViewProductServiceStore);
	},
	onAfterrender : function(view, eOpts) {
		this.bind();
		// User defined afterrender
	},
	onDestroy : function(view, eOpts) {
		// User defined destory
		var view = this.getMainView();
		if (view.SmfrViewProductionOrderServiceStore) {
			view.SmfrViewProductionOrderServiceStore.destroyStore();
			view.SmfrViewProductionOrderServiceStore = undefined;
		}
		if (view.SmfrViewProcessPathOperationServiceStore) {
			view.SmfrViewProcessPathOperationServiceStore.destroyStore();
			view.SmfrViewProcessPathOperationServiceStore = undefined;
		}
		if (view.SbasViewProductServiceStore) {
			view.SbasViewProductServiceStore.destroyStore();
			view.SbasViewProductServiceStore = undefined;
		}
		if (view.BasViewCommonDataSetupDetailServiceStore) {
			view.BasViewCommonDataSetupDetailServiceStore.destroyStore();
			view.BasViewCommonDataSetupDetailServiceStore = undefined;
		}
		if (view.SbasViewWorkshopServiceStore) {
			view.SbasViewWorkshopServiceStore.destroyStore();
			view.SbasViewWorkshopServiceStore = undefined;
		}
		if (view.SbasViewProductionLineServiceStore) {
			view.SbasViewProductionLineServiceStore.destroyStore();
			view.SbasViewProductionLineServiceStore = undefined;
		}
		if (view.SbasViewProductProcessPathServiceStore) {
			view.SbasViewProductProcessPathServiceStore.destroyStore();
			view.SbasViewProductProcessPathServiceStore = undefined;
		}
		if (this.getMainView().input)
			this.getMainView().input = undefined;
		if (this.getMainView().output)
			this.getMainView().output = undefined;
	},
	onCdvMatCodeSelect : function(btn, record) {
		var vals = this.getItem('frmBaseInfo').getValues();
		Ext.apply(vals, {
		    matCode: record.get('matCode'),
		    matVer: record.get('matVer'),
		    matDesc: record.get('matDesc'),
		    matSpec: record.get('matSpec'),
		    unit: record.get('unit')
		});
		this.getItem('frmBaseInfo').getForm().setValues(vals);
		
		this.loadProductProcessPathStore({
		    matCode: record.get('matCode'),
		    matVer: record.get('matVer')
		});
	},
	onCmbShopCodeSelect : function(combo, records, eOpts) {
		this.getItem('cmbLineCode').clearValue();
		
		this.loadProductionLineStore({
		    shopCode: records[0].get('shopCode')
		});
	},
	onCmbPathCodeChange : function(combobox, newValue, oldValue, eOpts) {
		this.loadPathOperationStore({
		    pathCode: newValue
		});
	},
	onBtnSearchClick : function(button, e, eOpts) {
		this.loadWorkOrderStore(this.getItem('frmSearch').getValues());
	},
	onBtnSearchResetClick : function(button, e, eOpts) {
		this.getItem('frmSearch').getForm().reset();
	},
	onGrdMoItemclick : function(dataview, record, item, index, e, eOpts) {
		this.getItem('frmBaseInfo').loadRecord(record);
	},
	onBtnResetClick : function(button, e, eOpts) {
		this.getItem('frmBaseInfo').getForm().reset();
	},
	onBtnSaveClick : function(button, e, eOpts) {
		if (!this.getItem('frmBaseInfo').isValid()) {
		    return;
		}
		
		var me = this;
		Ext.Msg.show({
		    title: T.label.save,
		    msg: T.msg.are_you_sure_to_save_data,
		    icon: Ext.Msg.QUESTION,
		    buttons: Ext.Msg.YESNO,
		    fn: function(btnId) {
		        if ('yes' == btnId) {
		            button.setDisabled(true);
		
		            YX.ZbusRpc.request({
		                module: 'SmfrSaveProductionOrderService',
		                showSuccess: true,
		                params: me.getItem('frmBaseInfo').getValues(),
		                success: function(rsp) {
		                    me.getItem('txtMo').setValue(rsp.mo);
		                    me.loadWorkOrderStore();
		                },
		                callback: function() {
		                    button.setDisabled(false);
		                }
		            });
		        }
		    }
		});
	},
	onBtnPrintClick : function(button, e, eOpts) {
	},
	loadWorkOrderStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('WorkOrderStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		if(store.loadPage){
			store.loadPage(1, options);
		}else{
			store.load(options);
		}
	},
	loadPathOperationStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('PathOperationStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		if(store.loadPage){
			store.loadPage(1, options);
		}else{
			store.load(options);
		}
	},
	loadProductListStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('ProductListStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		if(store.loadPage){
			store.loadPage(1, options);
		}else{
			store.load(options);
		}
	},
	loadCdsMoStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('CdsMoStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		if(store.loadPage){
			store.loadPage(1, options);
		}else{
			store.load(options);
		}
	},
	loadWorkShopStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('WorkShopStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		if(store.loadPage){
			store.loadPage(1, options);
		}else{
			store.load(options);
		}
	},
	loadProductionLineStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('ProductionLineStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		if(store.loadPage){
			store.loadPage(1, options);
		}else{
			store.load(options);
		}
	},
	loadProductProcessPathStore : function(params, options) {
		params = Ext.applyIf(params||{}, {
			// Default params
		});
		
		var store = this.getStore('ProductProcessPathStore');
		if(!store) {
			return false;
		}
		
		store.getProxy().extraParams = params;
		if(store.loadPage){
			store.loadPage(1, options);
		}else{
			store.load(options);
		}
	}
});